package com.be.beadmin.service.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.common.utils.ResultModel;
import com.be.beadmin.service.dto.input.FactoryCalendarDto;
import com.be.beadmin.service.dto.output.FactoryCalendarEx;
import com.be.beadmin.service.dto.output.FactoryCalendarExcelEx;
import com.be.beadmin.service.service.FactoryCalendarService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author chenwei
* @date 2023-02-22
**/
@RestController
public class FactoryCalendarController {

    @Autowired
    private  FactoryCalendarService factoryCalendarService;


    @PostMapping(value = "/service/getFactoryCalendarByPage")
    public ResultModel getFactoryCalendarByPage(@RequestBody PageEntity pageEntity)  {
        return ResultModel.success(factoryCalendarService.getFactoryCalendarByPage(pageEntity)) ;
    }

    @PostMapping("/service/addFactoryCalendar")
    public ResultModel addFactoryCalendar(@RequestBody FactoryCalendarDto factoryCalendarDto){
        factoryCalendarService.addFactoryCalendar(factoryCalendarDto);
        return ResultModel.success(ResultModel.ADD);
    }

    @PostMapping("/service/delFactoryCalendar")
    public ResultModel delFactoryCalendar(@RequestBody FactoryCalendarDto factoryCalendarDto){
        factoryCalendarService.delFactoryCalendar(factoryCalendarDto.getId());
        return ResultModel.success(ResultModel.DELETE);
    }


    @PostMapping("/service/updateFactoryCalendar")
    public ResultModel updateFactoryCalendar(@RequestBody FactoryCalendarDto factoryCalendarDto){
        factoryCalendarService.updateFactoryCalendar(factoryCalendarDto);
        return ResultModel.success(ResultModel.UPDATE);
    }


    @PostMapping("/service/exportFactoryCalendar")
    public void exportFactoryCalendar(HttpServletResponse response ,@RequestBody PageEntity pageEntity) throws IOException {
        Page<FactoryCalendarEx> factoryCalendarPage = factoryCalendarService.getFactoryCalendarByPage(pageEntity);
        List<FactoryCalendarEx> factoryCalendarExes = factoryCalendarPage.getContent();
        List<FactoryCalendarExcelEx> factoryCalendarExcelExes=factoryCalendarExes.stream().map(item->{
            FactoryCalendarExcelEx excel=new FactoryCalendarExcelEx();
            BeanUtils.copyProperties(item,excel);
            return excel;
        }).collect(Collectors.toList());
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("FactoryCalendar接口", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xls");
        //EasyExcel.write(response.getOutputStream(), FactoryCalendarExcelEx.class).sheet("sheet1").doWrite(factoryCalendarExcelExes);
        ExcelWriterBuilder ewb=EasyExcel.write(response.getOutputStream(), FactoryCalendarExcelEx.class);
        ewb.excelType(ExcelTypeEnum.XLS);
        ewb.sheet("sheet1").doWrite(factoryCalendarExcelExes);
    }
}